package db.entity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;

import db.BaseDAO;
import db.DAOFactory;
import db.Transaction;
import db.TransformInterface;

import model.Role;
import model.relation.Operation;

/**
 * 
 * Description: 对权限级别数据进行处理的DAO。
 * 
 * @since  2009/2/23
 * @author ashen
 * 
 */
public class RoleDAO extends BaseDAO implements TransformInterface<Role>{
	
	public RoleDAO(){
		setTableName("userscheme.role");
	}
	
	/**
	 * the method save of class RoleDAO
	 * 
	 * 该方法将参数权限级别对象存储到数据库
	 * 
	 * @param role 需要保存的Role对象
	 * @return 返回操作成功与否
	 */
	public boolean save(Role role){
		ArrayList<Operation> operations=role.getOperations();
		Transaction transaction=new Transaction();
		save(object2HashMap(role),transaction);
		
		Operation operation=null;
		if(operations!=null&&!operations.isEmpty())
			for(Iterator i=operations.iterator();i.hasNext();){
				operation=(Operation)i.next();
				DAOFactory.getOperationDAO().save(operation,transaction);
			}
		return transaction.commit();
	}
	
	/**
	 * the method update of class RoleDAO 
	 * 
	 * 该方法将参数权限级别更新到数据库
	 * 
	 * @param role 需要更新的Role对象
	 * @return 返回操作成功与否
	 */
	public boolean update(Role role){
		Transaction transaction=new Transaction();
		update(object2HashMap(role),"ID",role.getId(),transaction);
		ArrayList<Operation> operations=role.getOperations();
		DAOFactory.getOperationDAO().delete(role.getId(),transaction);
		if(!operations.isEmpty())
			for(Iterator i=operations.iterator();i.hasNext();){
				DAOFactory.getOperationDAO().save((Operation)i.next(),transaction);
			}
		return transaction.commit();
	}
	
	/**
	 * the method delete of class RoleDAO
	 * 
	 * 该方法删除指定权限级别ID的权限级别
	 * 
	 * @param id 需要删除的权限级别的ID
	 * @return 返回操作成功与否
	 */
	public boolean delete(int id){
		return delete("id",id);
	}
	
	/**
	 * the method findAll of class RoleDAO
	 * 
	 * 该方法返回所有权限级别
	 * 
	 * @return ArrayList<Role>类型，返回数据中所有权限级别
	 */
	public ArrayList<Role> findAll(){
		Transaction transaction=new Transaction();
		ArrayList<Role> roleList= list2ObjectList(super.findAllBase(transaction));
		transaction.close();
		return roleList;
	}

	/**
	 * the method findById of class RoleDAO
	 * 
	 * 该方法通过指定权限级别ID查找权限级别
	 * 
	 * @param id 权限级别ID
	 * @param transaction 数据库操作类Transaction的对象 
	 * @return 查询到的权限级别
	 */
	public Role findById(int id,Transaction transaction){
		return  hashMap2Object(findByPK("id",id,transaction));		 
	}
	
	/**
	 * the method findById of class RoleDAO
	 * 
	 * 该方法通过指定权限级别ID查找权限级别
	 * 
	 * @param id 权限级别ID
	 * @return 查询到的权限级别
	 */
	public Role findById(int id){
		Transaction transaction=new Transaction();
		Role role= hashMap2Object(findByPK("id",id,transaction));
		transaction.close();
		return role;
	}
	
	/**
	 * the method findById of class RoleDAO
	 * 
	 * 该方法通过指定权限级别name查找权限级别
	 * 
	 * @param name 权限级别name
	 * @return 查询到的权限级别
	 */
	public Role findByName(String name){
		Transaction transaction=new Transaction();
		Role role= hashMap2Object(findByPK("name",getFormatString(name),transaction));
		transaction.close();
		return role;
	}
	
	
	/**
	 * the method list2ObjectList of class RoleDAO
	 * 
	 * 该方法将存有权限级别信息的HashMap对象数组转换为Role对象数组
	 * 
	 * @param hashMapList HashMap对象数组
	 * @return Role对象数组
	 */
	public ArrayList<Role> list2ObjectList(ArrayList<HashMap> hashMapList){
		Role tempObject=null;
		ArrayList<Role> resultList=new ArrayList<Role>();
		if(hashMapList.isEmpty())
			return resultList;
		for(Iterator i=hashMapList.iterator();i.hasNext();){
			tempObject=hashMap2Object((HashMap)i.next());
			if(tempObject!=null)
				resultList.add(tempObject);
		}
		return resultList;
	}
	
	/**
	 * the method hashtable2Point of class RoleDAO
	 * 
	 * 该方法将HashMap类型的对象转换为Role的对象，并返回Role的对象.
	 * 
	 * @param hashMap 存储了一个权限级别信息的HashMap类型的对象
	 * @return Role的对象.
	 */
	public Role hashMap2Object(HashMap hashMap){
		if(hashMap==null)
			return null;
		Role role=new Role();
		Integer roleId=(Integer)hashMap.get("ID");
		role.setId(roleId);
		role.setName((String)hashMap.get("NAME"));	
		role.setDescription((String)hashMap.get("DESCRIPTION"));
		ArrayList<Operation> operations=DAOFactory.getOperationDAO().findByRoleId(roleId);
		role.setOperations(operations);
		return role;
	}
	
	/**
	 * the method object2HashMap of class RoleDAO
	 * 
	 * 该方法将参数Role对象转换为存储参数Role对象信息的HashMap对象，并返回
	 * 
	 * @param role 权限级别Role对象
	 * @return 存储参数权限操作Role对象信息的HashMap对象
	 */
	public HashMap object2HashMap(Role role){
		HashMap hashMap=new HashMap();
		hashMap.put("ID", "default");
		hashMap.put("NAME", getFormatString(role.getName()));
		hashMap.put("DESCRIPTION", getFormatString(role.getDescription()));
		return hashMap;
	}	
	
}